/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cconverter;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.*;
import org.xml.sax.InputSource;
import org.w3c.dom.*;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import org.xml.sax.SAXException;

/**
 *
 * @author SapunBoj
 */
public class Maps {

    public static Map<String, String> _cmap = new HashMap<String, String>(); // map for name of currency USD => US Dollars
    public static Map<String, String> _vmap = new HashMap<String, String>(); // map for value of currency USD => 23.36
    public static String lastUpdate;
    public final String cxml = "resources/currency.xml";
    public Pxml pxml = new Pxml();

    public Maps(){
        if(pxml.checkFile()){
            try {
                lastUpdate = pxml.getLastUpdate();
            } catch (Exception ex) {
                Logger.getLogger(Maps.class.getName()).log(Level.SEVERE, null, ex);
            }
        }else{
            lastUpdate = "Never";
        }
    }

//    fillCMap - fill _cmap with name of currency [key = > value]
    public void fillCMap() throws IOException, SAXException, ParserConfigurationException {
        String xml = readCXML();
        _cmap.clear();
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xml));
        Document doc = db.parse(is);
        NodeList nodes = doc.getElementsByTagName("item");
        for (int i = 0; i < nodes.getLength(); i++) {
            Element element = (Element) nodes.item(i);
            _cmap.put(element.getAttribute("currency"), element.getAttribute("name"));
        }

    }

    //fillVMap - fill _vmap with rate of currency
    public void fillVMap() throws FileNotFoundException, IOException, ParserConfigurationException, SAXException {
        if (new Pxml().checkFile()) {
            _vmap.clear();
            String xml = new Pxml().readFile();
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));
            Document doc = db.parse(is);
            NodeList nodes = doc.getElementsByTagName("Cube");
            for (int i = 0; i < nodes.getLength(); i++) {
                Element element = (Element) nodes.item(i);
                _vmap.put(element.getAttribute("currency"), element.getAttribute("rate"));
            }
        }// if file list.xml exists
    }

    // read cxml source file return string
    public String readCXML() throws IOException, SAXException {
        String ret = "";
        InputStream is = this.getClass().getResourceAsStream(cxml);
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line;
        while ((line = br.readLine()) != null) {
            ret += line + System.getProperty("line.separator","\n");
        }
        br.close(); // close reader
        return ret;
    }

    public void setLastUpdate(String lastUpdatei) throws SAXException, IOException, ParserConfigurationException {
        lastUpdate = lastUpdatei;
    }

    public String getLastUpdate() {
        return lastUpdate;
    }
}
